﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright company="Microsoft Corp." file="CancelJobCommand.cs">
//   All rights reserved. See License.txt in the project root for license information.
// </copyright>
// 
// --------------------------------------------------------------------------------------------------------------------
namespace HpcSchedulerManagement.Commands
{
    using System;

    using RestClient;

    public class CancelJobCommand : HpcSchedulerCommandBase
    {
        private readonly string hpcClusterName;

        private readonly int jobId;

        private readonly string cancellationMessage;

        private readonly bool forced;

        public CancelJobCommand(
                    IHpcSchedulerServiceContext serviceContext, 
                    string hpcClusterName, 
                    int jobId, 
                    string cancellationMessage, 
                    bool forced = false) : base(serviceContext)
        {
            if (string.IsNullOrEmpty(hpcClusterName))
            {
                throw new ArgumentException("Hpc cluster name must not be null or empty.", "hpcClusterName");
            }

            if (jobId <= 0)
            {
                throw new ArgumentException("Job id must not be less than or equal to 0.", "jobId");
            }

            this.hpcClusterName = hpcClusterName;
            this.jobId = jobId;
            this.cancellationMessage = cancellationMessage;
            this.forced = forced;
        }

        protected override RequestContext GetRequestContext()
        {
            return this.CreatePostOperationRequestContext();
        }

        protected override string FormatPath(IHpcSchedulerServiceContext serviceContext)
        {
            return string.Format("{0}/Job/{1}/Cancel", this.hpcClusterName, this.jobId);
        }

        protected override string FormatBody()
        {
            return string.Format("<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">{0}</string>", this.cancellationMessage);
        }

        protected override void AddParameters(RequestContext requestContext)
        {
            base.AddParameters(requestContext);

            requestContext.AddParameter(HpcSchedulerServiceQueryParameters.Forced, this.forced.ToString());
        }
    }
}